<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form ref="form" :model="formList" label-width="80px">
          <el-form-item label="关键字">
            <el-input
              v-model="formList.keyword"
              class="inputCol"
              placeholder="根据文章标题"
            ></el-input>
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="formList.state" placeholder="请选择">
              <el-option label="启用" :value="1"></el-option>
              <el-option label="禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-row>
            <el-button @click="clearList" class="colInput">清除</el-button>
            <el-button type="primary" @click="sousuo" class="colInput"
              >搜索</el-button
            >
          </el-row>
          <el-button
            type="primary"
            icon="el-icon-edit"
            class="colrigh"
            @click="showDialog = true"
            >新增技巧</el-button
          >
        </el-form>
        <div>
          <el-alert title="数据一共20条" type="info" show-icon> </el-alert>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="150">
          </el-table-column>
          <el-table-column label="文章标题" width="120">
            <template slot-scope="scope">
              {{ scope.row.title }}
              <i
                @click="video(scope.row.videoURL)"
                v-if="scope.row.videoURL"
                class="fa fa-film"
                aria-hidden="true"
              ></i>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数"> </el-table-column>
          <el-table-column prop="username" label="录入人"> </el-table-column>
          <el-table-column prop="createTime" label="录入时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseTime }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" :formatter="formatter">
            <template slot-scope="scope">
              {{ scope.row.state == 1 ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column prop="operation" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                style="font-size: 14px"
                class="loces"
                @click="preve(scope.row)"
                >预览</el-button
              >
              <el-button type="text" size="small" style="font-size: 14px"
              @click="changeState(scope.row)"
                >{{scope.row.state === 1 ? "禁用" : "启用"}}</el-button
              >
              <el-button
                type="text"
                size="small"
                style="font-size: 14px"
                @click="remove(scope.row)"
                :disabled="scope.row.state === 1"
                >删除</el-button
              >
              <el-button
                type="text"
                size="small"
                style="font-size: 14px"
                @click="editdel(scope.row)"
                :disabled="scope.row.state === 1"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formList.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="formList.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="10"
        >
        </el-pagination>
        <!-- 新增 -->
        <el-dialog title="新增文章" :visible.sync="showDialog">
          <el-form
            label-width="120px"
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
          >
            <el-form-item label="文章标题:" prop="title" class="elform">
              <el-input
                style="width: 90%"
                placeholder="请输入文章标签"
                v-model="ruleForm.title"
              />
            </el-form-item>
            <el-form-item label="文章内容:" prop="articleBody" class="elform">
              <div class="edit_container">
                <quill-editor
                  id="sor"
                  v-model="ruleForm.articleBody"
                  ref="myQuillEditor"
                  :options="editorOption"
                  @blur="onEditorBlur($event)"
                  @focus="onEditorFocus($event)"
                  @change="onEditorChange($event)"
                >
                </quill-editor>
              </div>
            </el-form-item>
            <el-form-item label="视频地址:" prop="videoURL" class="elform">
              <el-input
                style="width: 90%"
                placeholder="请输入视频地址"
                v-model="ruleForm.videoURL"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="sumiten">取消</el-button>
              <el-button type="primary" @click="submitForm(ruleForm)"
                >确定</el-button
              >
            </el-form-item>
          </el-form>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog title="新增文章" :visible.sync="editDialog">
          <el-form
            label-width="120px"
            :model="editForm"
            :rules="rules"
            ref="editForm"
          >
            <el-form-item label="文章标题:" prop="title" class="elform">
              <el-input
                style="width: 90%"
                placeholder="请输入文章标签"
                v-model="editForm.title"
              />
            </el-form-item>
            <el-form-item label="文章内容:" prop="articleBody" class="elform">
              <div class="edit_container">
                <quill-editor
                  id="sor"
                  v-model="editForm.articleBody"
                  ref="myQuillEditor"
                  :options="editorOption"
                  @blur="onEditorBlur($event)"
                  @focus="onEditorFocus($event)"
                  @change="onEditorChange($event)"
                >
                </quill-editor>
              </div>
            </el-form-item>
            <el-form-item label="视频地址:" prop="videoURL" class="elform">
              <el-input
                style="width: 90%"
                placeholder="请输入视频地址"
                v-model="editForm.videoURL"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="editDialog = false">取消</el-button>
              <el-button type="primary" @click="edit">确定</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </el-card>
    </div>
    <!-- 预览框 -->
    <el-dialog
      title="预览文章"
      :visible.sync="previewDialogVisible"
      width="43%"
    >
      <!-- 主体区域 -->
      <div class="preview-box">
        <div class="preview-title">{{ articlesInfo.title }}</div>
        <div class="preview-nav">
          <span>{{ articlesInfo.createTime }}</span>
          <span>{{ articlesInfo.username }}</span>
          <span class="el-icon-view"></span>
          <span>{{ articlesInfo.visits }}</span>
        </div>
        <div class="preview-content" v-html="articlesInfo.articleBody"></div>
      </div>
    </el-dialog>

    <div class="video" v-if="videoUrl">
      <div class="video-ico" @click="closeVideo()">
        <span class="el-icon-close"></span>
      </div>
      <div class="video-box">
        <video autoplay :src="videoUrl"></video>
      </div>
    </div>
  </div>
</template>

<script>
import {
  list as gain,
  remove,
  add,
  update,
  changeState
} from '../../api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'Articles',
  data () {
    return {
      formList: {
        keyword: '',
        state: '',
        page: 1,
        pagesize: 10
      },
      ruleForm: {
        title: '',
        articleBody: '',
        videoURL: null
      },
      videoUrl: null,
      editForm: {
        title: '',
        articleBody: '',
        videoURL: null,
        id: ''
      },
      editDialog: false,
      editorOption: {},
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 预览对话的框
      previewDialogVisible: false,
      // 点击当前预览的数据
      articlesInfo: [],
      allList: [],
      form: {
        region: ''
      },
      showDialog: false,
      input: '',
      // 文章信息
      tableData: [
        {
          //   title: '',
          //   visits: '',
          //   username: '',
          //   createTime: '',
          //   state: '',
          //   operation: ''
        }
      ]
    }
  },

  created () {
    this.gain()
  },
  components: {
    // eslint-disable-next-line no-undef
    quillEditor
  },
  methods: {
    video (row) {
      this.videoUrl = row
    },
    // 点击表格图标显示
    closeVideo () {
      this.videoUrl = null
    },
    handleSizeChange (newSize) {
      this.formList.pagesize = newSize
      this.gain()
    },
    handleCurrentChange (newPage) {
      this.formList.page = newPage
      this.gain()
    },
    clearList () {
      this.formList.keyword = ''
      this.formList.state = ''
      this.gain()
    },

    editdel (row) {
      this.editForm.title = row.title
      this.editForm.articleBody = row.articleBody
      this.editForm.videoURL = row.videoURL
      this.editForm.id = row.id
      this.editDialog = true
    },

    async edit () {
      try {
        await update(this.editForm)
        this.$message.success('修改文章成功')
        this.editDialog = false
        this.gain()
      } catch (e) {
        this.$message.success('修改文章失败')
      }
    },
    preve (row) {
      this.articlesInfo = row
      // console.log(this.articlesInfo)
      this.previewDialogVisible = true
    },

    // 删除
    async remove (row) {
      const res = await this.$confirm(
        '此操作将删除这条数据, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (res === 'cancel') return this.$message.info('已取消')

      await remove(row)
      // console.log(res1)

      //   if (!res1.success) return this.$message.error(res.message)
      //   // 编辑成功，给用户进行提示
      this.$message.success('已删除')
      //   //   重新调用数据
      this.gain()
    },

    async changeState (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      // console.log(res)
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
    },

    formatter (row, column) {
      if (row.state === 1) {
        return '已开启'
      } else if (row.state === 0) {
        return '已禁用'
      }
      return ''
    },
    async gain () {
      const res = await gain({
        page: this.formList.page,
        pagesize: this.formList.pagesize
      })
      // console.log(res)
      this.tableData = res.data.items
    },
    async sousuo () {
      const res = await gain(this.formList)
      console.log(res)
      this.tableData = res.data.items
    },
    sumiten () {
      this.showDialog = false
    },
    submitForm () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          await add(this.ruleForm)
          this.$message.success('新增文章成功')
          this.gain()
          this.showDialog = false
        } else {
          this.$message.success('新增文章失败')
          return false
        }
      })
    },
    onEditorReady (editor) {
      // 准备编辑器
    },
    onEditorBlur () {}, // 失去焦点事件
    onEditorFocus () {}, // 获得焦点事件
    onEditorChange () {} // 内容改变事件
  },
  computed: {
    editor () {
      return this.$refs.myQuillEditor.quill
    }
  }
}
</script>

<style scoped lang="scss">
.colrigh {
  float: right;
  height: 35px;
  margin-left: 10px;
}
.el-input {
  width: 200px;
}
.video {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
.fa-film:before {
  color: #0245a8;
}
.preview-box {
  .preview-title {
    font-size: 25px;
    font-weight: 700;
    margin-bottom: 10px;
  }
  .preview-nav {
    span {
      margin-right: 10px;
    }
  }
  .preview-content {
    margin-top: 10px;
    padding: 10px;
    background-color: #f5f5f5;
    border-top: 1px dashed #ccc;
  }
}
.el-row {
  display: inline-block;
}
.colInput {
  height: 35px;
  margin-left: 20px;
}

.app-container {
  width: 100%;
}
.el-form-item {
  display: inline-block;
}
.elform {
  width: 100%;
}
.el-pagination {
  float: right;
  margin-top: 20px;
}
.inputCol {
  width: 200px;
}
.loces {
  display: inline-block;
}
</style>
